package com.frkj.order.config;

import com.frkj.order.cache.BusinessCache;
import com.frkj.order.cache.ConfigCache;
import com.frkj.order.cache.LoginOperatorCache;
import com.frkj.order.controller.*;
import com.frkj.order.model.*;
import com.frkj.order.util.TimerJob;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;


/**
 * API引导式配置
 */
public class MainConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		loadPropertyFile("config.properties");

		me.setMainRenderFactory(new BeetlRenderFactory());

		GroupTemplate gt = BeetlRenderFactory.groupTemplate;
		// 可以通过gt设置全局共享变量

		me.setDevMode(PropKit.getBoolean("devMode", false));
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		//me.add("/", IndexController.class, "/index");	// 第三个参数为该Controller的视图存放路径

		me.add("/demo", DemoController.class);
		me.add("/queue", QueueController.class);
		me.add("/admin/operator", OperatorController.class);
		me.add("/admin/business", BusinessController.class);
		me.add("/admin/userorder", UserOrderController.class);
		me.add("/admin/historyorder", HistoryOrderController.class);
		me.add("/admin/loginoperator", LoginOperatorController.class);
    }
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("operator", Operator.class);
		arp.addMapping("business", Business.class);
		arp.addMapping("userorder", "number", UserOrder.class);
		arp.addMapping("historyorder", HistoryOrder.class);
		arp.addMapping("config", Config.class);
		arp.addMapping("loginoperator", "window", LoginOperator.class);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		BusinessCache.loadData();
		ConfigCache.loadData();
		LoginOperatorCache.loadData();

		startScheduleJob();
	}

	private void startScheduleJob() {
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			JobDetail jobDetail = JobBuilder.newJob(TimerJob.class).withIdentity("job-1").build();

			String cronExp = "0 0/1 * 1/1 * ? *"; // 每分钟执行（测试用）
			//String cronExp = "0 0 0 1/1 * ? *"; // 每天凌晨执行
			CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("triger-1").withSchedule(CronScheduleBuilder.cronSchedule(cronExp)).forJob(jobDetail).build();

			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 8080, "/", 5);
	}
}
